package com.kingdee.eas.xr.helper;

import java.awt.Color;

public class ColorUtil {

    public ColorUtil()
    {
    }
    public static String getColorDesc(Color color)
    {
        StringBuffer desc = new StringBuffer();
        desc.append("RGB{").append(color.getRed());
        desc.append(",").append(color.getGreen());
        desc.append(",").append(color.getBlue()).append("}");
       return desc.toString();
    }
    public static Color parseColor(String colorDesc)
    {

       if(colorDesc == null)
       return Color.WHITE;
       if(!colorDesc.matches("^(?i)RGB\\{\\d{1,3}\\s*,\\d{1,3}\\s*,\\d{1,3}\\s*\\}$"))

          throw new IllegalArgumentException("Invaild Color format, a right format \uFF1ARGB{r,g,b}");

       int begin = colorDesc.indexOf('{') + 1;
       int end = colorDesc.indexOf('}');
       if(begin < 0 || end < 0)
        {           return Color.WHITE;
        } else
        {
           String rgb[] = colorDesc.substring(begin, end).split(",");
           int r = Integer.parseInt(rgb[0].trim());
           int g = Integer.parseInt(rgb[1].trim());
           int b = Integer.parseInt(rgb[2].trim());
          return new Color(r, g, b);
        }
    }
    public static Color parseColor(Object colorDesc)
    {
       if(colorDesc != null)
           return parseColor(colorDesc.toString());      
       else           return parseColor(((String) (null)));
    }

}
